#include <iostream>

using namespace std;

int n, m, ans = 0;
char map[102][12];
bool put[102][12] = {false};
int canputcount = 0;
int *dp;
int **canput;

int max(int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}

int located(int x, int y) {
    for (int i = 0; i < canputcount; i++) {
        if (x == canput[i][0] && y == canput[i][1]) {
            return i;
        }
    }
    return -1;
}

int* scan(int index) {
    int count = 0;
    int *arr = new int[8];
    for (int i = 0; i < 8; i++) {
        arr[i] = -1;
    }
    if (canput[index][0]-1 > 0 && put[canput[index][0]-1][canput[index][1]] == true) {
        arr[count] = located(canput[index][0]-1,canput[index][1]);
        count += 1;
    }
    if (canput[index][0]-2 > 0 && put[canput[index][0]-2][canput[index][1]] == true) {
        arr[count] = located(canput[index][0]-2,canput[index][1]);
        count += 1;
    }
    if (canput[index][0]+1 <= n && put[canput[index][0]+1][canput[index][1]] == true) {
        arr[count] = located(canput[index][0]+1,canput[index][1]);
        count += 1;
    }
    if (canput[index][0]+2 <= n && put[canput[index][0]+2][canput[index][1]] == true) {
        arr[count] = located(canput[index][0]+2,canput[index][1]);
        count += 1;
    }
    if (canput[index][1]-1 > 0 && put[canput[index][0]][canput[index][1]-1] == true) {
        arr[count] = located(canput[index][0],canput[index][1]-1);
        count += 1;
    }
    if (canput[index][1]-2 > 0 && put[canput[index][0]][canput[index][1]-2] == true) {
        arr[count] = located(canput[index][0],canput[index][1]-2);
        count += 1;
    }
    if (canput[index][1]+1 <= n && put[canput[index][0]][canput[index][1]+1] == true) {
        arr[count] = located(canput[index][0],canput[index][1]+1);
        count += 1;
    }
    if (canput[index][1]+2 <= n && put[canput[index][0]][canput[index][1]+2] == true) {
        arr[count] = located(canput[index][0],canput[index][1]+2);
        count += 1;
    }
    // for (int i = 0; i < 8; i++) {std::cout << index << " " << arr[i] << std::endl;}
    return arr;
}

void DFS(int index, int total, int count) {
    if (index >= count) {
        ans = max(ans,count);
        return;
    }
    if (put[canput[index][0]][canput[index][1]] == false) {
        DFS(index+1,total,count);
    }
    else {
        // std::cout << index << " " << count << std::endl;
        int* tmp = scan(index);
        for (int i = 0; i < 8; i++) {
            if (tmp[i] != -1) {
                put[canput[tmp[i]][0]][canput[tmp[i]][1]] = false;
                DFS(index+1,total,count+1);
                put[canput[tmp[i]][0]][canput[tmp[i]][1]] = true;
                DFS(index+1,total,count);
            }
        }
    }
}

int main() {
    std::cin >> n >> m;
    char* inputarr = new char[m];
    for (int i = 1; i <= n; i++) {
        std::cin >> inputarr;
        for (int j = 0; j < m; j++) {
            map[i][j+1] = inputarr[j];
            if (inputarr[j] == 'P') {
                put[i][j+1] = true;
                canputcount += 1;
            }
            // std::cout << put[i][j+1];
        }
        // std::cout << std::endl;
    }
    // std::cout << canputcount << std::endl;
    canput = new int*[canputcount];
    for (int i = 0; i < canputcount; i++) {
        canput[i] = new int[2];
    }
    int tmp = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (put[i][j] == true) {
                canput[tmp][0] = i;
                canput[tmp][1] = j;
                tmp += 1;
            }
        }
    }
    if (canputcount > 0) {
        DFS(0,canputcount,1);
    }
    std::cout << ans << std::endl;
    return 0;
}
